export INSTALLPATH=${shell pwd}/install
export MEDIAINC = -I${shell pwd}/src/source/ -I${shell pwd}/src/sink/ -I${shell pwd}/src/server/ -I${shell pwd}/src/tools/ -I${shell pwd}/inc/ -I${shell pwd}
export FFMPEGCFLAGS=-I${INSTALLPATH}/include/
export FFMPEGLIBS=-L${INSTALLPATH}/lib/ -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -fvisibility=hidden -Wl,-Bsymbolic
export UMAPIPATH = ${INSTALLPATH}/include/umapi
export INIPARSERPATH = ${INSTALLPATH}/include/iniparser

LOCALPATH=${shell pwd}/src
SRC_DIRS := $(shell find $(LOCALPATH) -maxdepth 4 -type d )
C_SRC_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
CPP_SRC_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.cpp))
ALL_SRC_FILES = $(CPP_SRC_FILES) $(C_SRC_FILES)
ALL_SRC_FILES += main.c iniConf.c umAvTask.c
OBJS = $(foreach n,$(ALL_SRC_FILES),$(addsuffix .o,$(basename ${n})))
#export PQ=-pg
CFLAGS=-minline-all-stringops -march=nocona
DEMO = umMediaServer

all:pre umapi iniparser ffmpeg  demo
pre::
	mkdir $(INSTALLPATH)/include -p
	mkdir $(INSTALLPATH)/include/iniparser -p
	mkdir $(INSTALLPATH)/lib -p
umapi:: pre
	make -C umapi install
iniparser:: pre
	make -C iniparser-master
	mkdir $(INSTALLPATH)/lib/iniparser -p
	cp iniparser-master/libiniparser.a $(INSTALLPATH)/lib -f
	cp iniparser-master/src/*.h $(INSTALLPATH)/include/iniparser -f
ffmpeg:: 
	make -C ffmpeg-4.2.2 -f build.sh
demo:: pre $(OBJS)
	rm $(INSTALLPATH)/lib/*.so* -f
	g++ $(OBJS)  -Wall -std=c11 -g -o $(DEMO) -L$(UMAPIPATH)/../../lib/ -lumapi $(FFMPEGLIBS) $(FFMPEGCFLAGS)  -liniparser -ldl -lz -lm  -latomic -lpthread $(PQ)  -Wall -std=c11
	strip $(DEMO)
clean:umapi_clean iniparser_clean ffmpeg_clean libfaac_clean demo_clean
umapi_clean:
	make -C umapi clean
iniparser_clean:
	make -C iniparser-master clean
ffmpeg_clean:
	cd ffmpeg-4.2.2;make distclean;cd -
libfaac_clean:
	cd libfaac-master;make distclean;cd -
demo_clean:
	rm $(OBJS) -f
	rm $(DEMO) -f
%.o : %.c 
	@echo "build " $@
	@$(CROSS)gcc -g -O2 -fPIC -c -D_GNU_SOURCE $(MEDIAINC) -I$(UMAPIPATH)/include -I$(UMAPIPATH)/include/os/ -I$(UMAPIPATH)/include/errcode/ \
			$(FFMPEGCFLAGS) -I$(LOCALPATH)/../ffmpeg-4.2.2 -I$(LOCALPATH) -I$(INIPARSERPATH)/ $< -o $@  -lm >/dev/null -std=c11 $(PQ) \
			$(CFLAGS)
